home *** CD-ROM | disk | FTP | other *** search
-
- /*
- ** File: tree.c
- ** Author: Karen Foltz
- ** Last Modified: 10 Oct 1986
- ** Purpose:
- ** The module "tree" defines the tree data structure which describes
- ** objects in the Constructive Solid Geometry modelling system, and
- ** a set of operations on these data structures. The set of operations
- ** defined are used to build and to print the contents of a CSG tree.
- */
-
- #include <stdio.h>
- #include "4D.h"
- #include "tree.h"
-
- /*Imports*/
- extern char *malloc();
-
-
- static char *PrimitiveObjNames[] = {
- "sphere",
- "cube",
- "cone",
- "cylinder",
- "halfspace",
- "torus",
- "mesh",
- };
-
- static char *OperatorNames[] = {
- "Union",
- "Intersection",
- "Difference",
- };
-
-
- CSGtree *MakePrimitiveNode(shape, embed, invembed, surface)
- PrimitiveShape shape;
- TransformType4D embed, invembed;
- MaterialDescr surface;
- {
- CSGtree *newnode;
- newnode = (CSGtree *) malloc(sizeof(CSGtree));
- newnode->kind = Primitive;
- newnode->embedding = embed;
- newnode->invembedding = invembed;
- newnode->var.p.shape = shape;
- newnode->var.p.surface = surface;
- return newnode;
- }
-
- CSGtree *MakeCompositeNode(op, l_solid, r_solid, embed, invembed)
- enum OpCodes op;
- CSGtree *l_solid, *r_solid;
- TransformType4D embed, invembed;
- {
- CSGtree *newnode;
- newnode = (CSGtree *) malloc(sizeof(CSGtree));
- newnode->kind = Composite;
- newnode->embedding = embed;
- newnode->invembedding = invembed;
- newnode->var.c.operation = op;
- newnode->var.c.l_solid_ptr = l_solid;
- newnode->var.c.r_solid_ptr = r_solid;
- return newnode;
- }
-
- CSGtree *EmbedNode(node, embed, invembed)
- CSGtree *node;
- TransformType4D embed,invembed;
- {
- node->embedding = TxT4D(embed,node->embedding);
- node->invembedding = TxT4D(node->invembedding,invembed);
- return node;
- }
-
- CSGtree *DuplicateTree(tree)
- CSGtree *tree;
- {
- CSGtree *newtree, oldtree;
- oldtree = *tree;
- newtree = (CSGtree *) malloc(sizeof(CSGtree));
- newtree->kind = oldtree.kind;
- newtree->embedding = oldtree.embedding;
- newtree->invembedding = oldtree.invembedding;
- if (oldtree.kind == Primitive) {
- newtree->var.p.shape = oldtree.var.p.shape;
- newtree->var.p.surface = oldtree.var.p.surface;
- } else {
- newtree->var.c.operation = oldtree.var.c.operation;
- newtree->var.c.l_solid_ptr =
- DuplicateTree(oldtree.var.c.l_solid_ptr);
- newtree->var.c.r_solid_ptr =
- DuplicateTree(oldtree.var.c.r_solid_ptr);
- }
- return newtree;
- }
-
-
- CSGtree *SetSurface(node,surface)
- CSGtree *node;
- MaterialDescr surface;
- {
- if (node->kind == Composite) {
- fprintf(stderr,"ERROR in SetSurface: node is not Primitive.\n");
- exit(-1);
- }
- node->var.p.surface = surface;
- return node;
- }
-
- void PrintOpcode(f,op)
- enum OpCodes op;
- FILE *f;
- {
- fprintf(f, "%s\n", OperatorNames[(int) op]);
- }
-
- void PrintPrimitiveObject(f,object)
- FILE *f;
- enum PrimitiveObjects object;
- {
- fprintf(f, "%s\n", PrimitiveObjNames[(int) object]);
- }
-
- void PrintNodeType(f,type)
- FILE *f;
- enum NodeTypes type;
- {
- if (type == Primitive)
- fprintf(f,"Primitive\n");
- else
- fprintf(f,"Composite\n");
- }
-
- /*
- * PrintMaterial:
- * Print out the record describing a material. Prints the texture
- * function name and what its parameters are, and the pair of embedding
- * matrices that do the transformations from texture to object space and back.
- */
- void
- PrintMaterial(f, material)
- FILE *f;
- MaterialDescr material;
- {
- fprintf(f,"Material Description:\n");
- fprintf(f, "Material is%s an emitter.\n", material.emitter ? "" : " not");
- fprintf(f, "embedding matrix into texture space:\n");
- PrintTransform4D(f, material.embedding);
- fprintf(f, "inverse embedding matrix:\n");
- PrintTransform4D(f, material.inv_embedding);
- fprintf(f, "function: \n");
- if (material.texture) {
- fPrintToken(f, material.texture);
- fprintf(f, "\n");
- } else
- fprintf(f, "NONE\n");
- }
-
-
-
- void PrintNodeSurface(f,node)
- FILE *f;
- CSGtree *node;
- {
- if (node->kind == Composite) {
- fprintf(stderr,"ERROR in PrintNodeSurface: node is not Primitive.\n");
- exit(-1);
- }
-
- PrintMaterial(f,node->var.p.surface);
- }
-
-
- void PrintBoundVolume(f,bound_volume)
- FILE *f;
- BoundingVolume bound_volume;
- {
- int i;
-
- #ifdef BOUNDING_SPHERES
- fprintf(f,"Bounding Volume: radius = %7.2f center = ",
- bound_volume.radius);
- PrintPoint4D(f, bound_volume.center);
- #else
- fprintf( f, "\
- Bounding Volume: (xmin,xmax) (ymin,ymax) (zmin,zmax)\n\
- \n");
- for(i=0; i<3; i++)
- fprintf( f,"(%f,%f) ", bound_volume.I[i].min, bound_volume.I[i].max );
- #endif
- }
-
- unsigned int PrintNode(f,count,ptr)
- FILE *f;
- unsigned int count;
- CSGtree *ptr;
- {
- fprintf(f,"\nNode: %u \t",count);
- if (ptr->kind == Composite) {
- fprintf(f,"Composite Node, operator = ");
- PrintOpcode(f, ptr->var.c.operation );
- } else {
- fprintf(f,"Primitive Node, ");
- PrintPrimitiveObject(f, ptr->var.p.shape.shape );
- PrintNodeSurface(f,ptr);
- }
- fprintf(f,"embedding matrix:\n");
- PrintTransform4D(f, ptr->embedding);
- fprintf(f,"inverse embedding matrix:\n");
- PrintTransform4D(f, ptr->invembedding);
- PrintBoundVolume(f, ptr->bound_volume);
- fprintf(f,"\n");
- if (ptr->kind == Composite) {
- count = PrintNode(f,count+1,ptr->var.c.l_solid_ptr);
- count = PrintNode(f,count+1,ptr->var.c.r_solid_ptr);
- }
- return count;
- }
-
- void PrintCSGtree(f,ptr)
- CSGtree *ptr;
- FILE *f;
- {
- fprintf(f,"\nPre-order traversal of CSG tree:\n");
- PrintNode(f,0,ptr);
- fprintf(f,"End of Tree.\n");
- }
-
-